package com.inei.cie;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.dao.PrincipalDao;
import com.inei.cie.entidades.Car;
import com.inei.cie.entidades.Dist;
import com.inei.cie.entidades.Dpto;
import com.inei.cie.entidades.Mod;
import com.inei.cie.entidades.PCar;
import com.inei.cie.entidades.PMod;
import com.inei.cie.entidades.Prov;
import com.inei.cie.utilitarios.ListadoCargaAdapter;
import com.inei.cie.utilitarios.ListadoGeneralLocalesAdapter;
import com.inei.cie.utilitarios.Sesion;
import com.inei.cie.utilitarios.Variables;

public class Asignar_Ruta_Activity extends Activity {
	
	public Variables var;

	public List<PCar> lPCar;
	public List<PMod> lPMod;
	public List<PCar> lPCarTemp;
	public List<Car> lCar;
	public List<Mod> lMod;
	public List<Car> lCarTemp;
	public List<Dpto> lDpto;
	public List<Prov> lProv;
	public List<Dist> lDist;

	public Car car;
	public Mod mod;

	public PCar pcar;
	public PMod pmod;

	public PrincipalDao principal;
	public ListView lvPadron, lvCarga;
	public TextView tvDepartamento, tvDNI, tvCP;

	public EditText etBuscarPad, etBuscarCar;
	public Bundle b;
	public int dep;
	public String dni;
	public Sesion sesion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.asignar_ruta);
		IniciarComponentes();
	}

	public void IniciarComponentes() {
		var = new Variables();
		b = getIntent().getExtras();
		dep = 0;
		dep = b.getInt(var.DEP);
		dni = b.getString(var.DNI);

		sesion = new Sesion(this);
		principal = new PrincipalDao(this);
		lDpto = principal.ListadoDepartamentos();
		lProv = principal.ListadoProvincia();
		lDist = principal.ListadoDistrito();
		lCar = principal.ListadoGeneralLocales(dep);
		lMod = principal.ListadoGeneralModulares(dep);

		lPCar = principal.ListadoLocales();
		lPMod = principal.ListadoModulares();

		lvPadron = (ListView) findViewById(R.id.lvPadron);
		lvCarga = (ListView) findViewById(R.id.lvCarga);

		tvDepartamento = (TextView) findViewById(R.id.tvDepartamento);
		tvDepartamento.setText(lDpto.get(dep - 1).getNombre());
		tvDNI = (TextView) findViewById(R.id.tvDNI);
		tvDNI.setText(dni);
		tvCP = (TextView) findViewById(R.id.tvCP);
		tvCP.setText(sesion.getCP());

		etBuscarPad = (EditText) findViewById(R.id.etBuscarPad);
		etBuscarCar = (EditText) findViewById(R.id.etBuscarCar);

		lCarTemp = lCar;
		if (lCar != null) {
			ListadoGeneralLocalesAdapter adaptador = new ListadoGeneralLocalesAdapter(
					this, lCar, lDpto, lProv, lDist);
			lvPadron.setAdapter(adaptador);

			etBuscarPad.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					FiltrarListaPad(s.toString());
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
				}

				@Override
				public void afterTextChanged(Editable s) {
				}
			});
		}
		lvPadron.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View view,
					int pos, long arg3) {
				CrearDialogoAgregar(pos);
				return false;
			}
		});

		lPCarTemp = lPCar;
		if (lPCar != null) {
			ListadoCargaAdapter adaptadorC = new ListadoCargaAdapter(this,
					lPCar, lDpto, lProv, lDist);
			lvCarga.setAdapter(adaptadorC);

			etBuscarCar.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					FiltrarListaCar(s.toString());
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
				}

				@Override
				public void afterTextChanged(Editable s) {
				}
			});
		}

		lvCarga.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View view,
					int pos, long arg3) {
				CrearDialogoEliminar(pos);
				return false;
			}
		});

	}

	public void CrearDialogoAgregar(int pos) {
		car = lCarTemp.get(pos);

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setCancelable(false);
		builder.setTitle("C\u00f3digo de Local Nro. " + car.getId_local());
		String p = "";
		for (Prov prov : lProv) {
			if (prov.getCod_dep() == car.getCod_dep()
					&& prov.getCod_prov() == car.getCod_prov()) {
				p = prov.getNombre();
				break;
			}
		}
		String d = "";
		for (Dist dist : lDist) {
			if (dist.getCod_dep() == car.getCod_dep()
					&& dist.getCod_prov() == car.getCod_prov()
					&& dist.getCod_dist() == car.getCod_dist()) {
				d = dist.getNombre();
				break;
			}
		}
		builder.setMessage(" DEPARTAMENTO:\t\t"
				+ lDpto.get(dep - 1).getNombre() + "\n"
				+ " PROVINCIA:\t\t\t\t\t" + p + "\n" + " DISTRITO:\t\t\t\t\t"
				+ d);
		builder.setPositiveButton("Agregar",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (!principal.AgregarLocal(car, lMod)) {
							Toast.makeText(Asignar_Ruta_Activity.this,
									"No se pudo agregar, este local ya existe",
									Toast.LENGTH_SHORT).show();
						} else {
							lPCar = principal.ListadoLocales();
							lPCarTemp = lPCar;
							ListadoCargaAdapter adaptadorC = new ListadoCargaAdapter(
									Asignar_Ruta_Activity.this, lPCar, lDpto,
									lProv, lDist);
							lvCarga.setAdapter(adaptadorC);
							Toast.makeText(Asignar_Ruta_Activity.this,
									"Se agreg\u00f3 correctamente",
									Toast.LENGTH_LONG).show();
						}
					}
				});
		builder.setNegativeButton("Cancelar",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
		builder.create().show();
	}

	public void CrearDialogoEliminar(int pos) {
		pcar = lPCarTemp.get(pos);

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setCancelable(false);
		builder.setTitle("C\u00f3digo de Local Nro. " + pcar.getId_local());
		String dep = "";

		for (Dpto dpto : lDpto) {
			if (dpto.getCod_dep() == pcar.getPC_A_1_Dep()) {
				dep = dpto.getNombre();
				break;
			}
		}
		String p = "";
		for (Prov prov : lProv) {
			if (prov.getCod_dep() == pcar.getPC_A_1_Dep()
					&& prov.getCod_prov() == pcar.getPC_A_2_Prov()) {
				p = prov.getNombre();
				break;
			}
		}
		String d = "";
		for (Dist dist : lDist) {
			if (dist.getCod_dep() == pcar.getPC_A_1_Dep()
					&& dist.getCod_prov() == pcar.getPC_A_2_Prov()
					&& dist.getCod_dist() == pcar.getPC_A_3_Dist()) {
				d = dist.getNombre();
				break;
			}
		}
		builder.setMessage(" DEPARTAMENTO:\t\t" + dep + "\n"
				+ " PROVINCIA:\t\t\t\t\t" + p + "\n" + " DISTRITO:\t\t\t\t\t"
				+ d);
		builder.setPositiveButton("Eliminar",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (principal.EliminarLocal(pcar)) {
							lPCar = principal.ListadoLocales();
							lPCarTemp = lPCar;
							if (lPCar != null) {
								ListadoCargaAdapter adaptadorC = new ListadoCargaAdapter(
										Asignar_Ruta_Activity.this, lPCar,
										lDpto, lProv, lDist);
								lvCarga.setAdapter(adaptadorC);
							} else {
								lvCarga.setAdapter(null);
							}

							Toast.makeText(Asignar_Ruta_Activity.this,
									"Local escolar Nro. " + pcar.getId_local(),
									Toast.LENGTH_SHORT).show();
						}
					}
				});
		builder.setNegativeButton("Cancelar",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
		builder.create().show();
	}

	public void FiltrarListaPad(String s) {
		lCarTemp = new ArrayList<Car>();
		for (Car car : lCar) {
			if (car.getId_local().contains(s)) {
				lCarTemp.add(car);
			}
		}
		ListadoGeneralLocalesAdapter adaptador = new ListadoGeneralLocalesAdapter(
				this, lCarTemp, lDpto, lProv, lDist);
		lvPadron.setAdapter(adaptador);
	}

	public void FiltrarListaCar(String s) {
		lPCarTemp = new ArrayList<PCar>();
		for (PCar pcar : lPCar) {
			if (pcar.getId_local().contains(s)) {
				lPCarTemp.add(pcar);
			}
		}
		ListadoCargaAdapter adaptadorC = new ListadoCargaAdapter(this,
				lPCarTemp, lDpto, lProv, lDist);
		lvCarga.setAdapter(adaptadorC);
	}

	@Override
	public void onBackPressed() {
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_asignar, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir ?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnTerminar:
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
